﻿using Inaction.Core;
using Inaction.Criteria;
using Inaction.UI.Common;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace Inaction.UI
{
    [ToolboxItem(false), System.ComponentModel.DesignTimeVisible(false)]
    [Designer(typeof(Inaction.UI.ComponentModel.Design.ComponentDesigner))]
    public class BindingInfo : System.ComponentModel.Component
    {
        #region 属性

        #region UI控制
        private bool inQuery = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("是否由BarManager查询数据")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool InQuery
        {
            get { return inQuery; }
            set { inQuery = value && this.IsRoot; }
        }

        bool lastColumnEnterToAddnew = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("编辑状态,表格控件在最后一列按下回车键时,是否新增一行数据")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool LastColumnEnterToAddnew
        {
            get { return lastColumnEnterToAddnew; }
            set { lastColumnEnterToAddnew = value; }
        }

        bool lastEditorEnterToAddnew = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("编辑状态,最后一个数据项的输入控件中按下回车键时,是否新增一行数据")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool LastEditorEnterToAddnew
        {
            get { return TypeIsList && lastEditorEnterToAddnew; }
            set { lastEditorEnterToAddnew = value; }
        }

        private bool queryOnLoad = false;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(false)]
        [Description("是否在Form加载时查询数据")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool QueryOnLoad
        {
            get { return queryOnLoad && this.IsRoot; }
            set { queryOnLoad = value; }
        }

        bool allowEdit = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("是否可编辑数据,当数据源的AllowEdit=false时,其值必为false")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool AllowEdit
        {
            get
            {
                if (DesignMode) return allowEdit;
                return allowEdit && CurrentBusiness != null && CurrentBusiness.AllowEdit;
            }
            set { allowEdit = value; }
        }

        internal bool TypeIsList
        {
            get { return ListType != null && ListType.GetInterface(typeof(Business.IEntityList).FullName) != null; }
        }

        bool allowNew = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("是否可新增数据,当数据源的AllowNew=false时,其值必为false")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool AllowNew
        {
            get
            {
                if (DesignMode) return allowNew;
                if (!allowNew) return false;
                if (TypeIsList) return EntityList != null && EntityList.AllowNew;
                else
                {
                    var error = string.Empty;
                    return CurrentBusiness == null || (CurrentBusiness as IDataErrorInfo).Error == string.Empty;
                }
            }
            set { allowNew = value; }
        }

        bool allowRemove = true;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(true)]
        [Description("是否可删除数据,当数据源的AllowRemove=false时,其值必为false")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool AllowRemove
        {
            get
            {
                if (DesignMode) return allowRemove;
                if (!allowRemove || EntityList == null || CurrentBusiness == null) return false;
                return EntityList.AllowRemove && CurrentBusiness.AllowRemove;
            }
            set { allowRemove = value; }
        }

        bool allowGridEdit = false;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(false)]
        [Description("编辑状态时，是否可在列表控件中编辑数据,通过设置列表控件的Editable属性来实现")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public bool AllowGridEdit
        {
            get { return allowGridEdit; }
            set { allowGridEdit = value; }
        }
        #endregion

        #region 数据
        [Category("Inaction")]
        [Description("是否为根数据源")]
        public bool IsRoot
        {
            get
            {
                return BindingSource != null && BindingSource == RootSource;
            }
        }

        TransDataType transDataType = TransDataType.Customer;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(TransDataType.Customer)]
        [Description("事务数据类型")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public TransDataType TransDataType
        {
            get
            {
                if (rootSource == this.BindingSource)
                    return TransDataType.Customer;
                return transDataType;
            }
            set { transDataType = value; }
        }

        BindingSource rootSource = null;
        [Category("Inaction")]
        [Browsable(true)]
        [DefaultValue(null)]
        [Description("根数据源")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public BindingSource RootSource
        {
            get { return rootSource; }
            set { rootSource = value; }
        }

        BindingSource bindingSource = null;
        [Category("Inaction")]
        [Browsable(true)]
        [Description("数据源")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public BindingSource BindingSource
        {
            get { return bindingSource; }
            set
            {
                if (bindingSource == value) return;
                if (value != null && DesignMode)
                {
                    if (this.Site.Name.StartsWith("bindingInfo", StringComparison.OrdinalIgnoreCase))
                    {
                        var itemType = BindingHelper.GetListItemType(value);
                        var siteName = itemType.Name[0].ToString().ToLower() + itemType.Name.Substring(1) + "BindingInfo";
                        if (Site.Container.Components[siteName] == null)
                        {
                            Site.Name = siteName;
                        }
                    }
                }
                bindingSource = value;
                InitRootSource();
            }
        }

        BindingSource criteriaBindingSource = null;
        [Category("Inaction")]
        [DefaultValue(null)]
        [Browsable(true)]
        [Description("查询数据源,只有根数据源才有查询条件")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public BindingSource CriteriaBindingSource
        {
            get { return criteriaBindingSource; }
            set
            {
                if (criteriaBindingSource == value) return;
                if (DesignMode)
                {
                    var type = BindingHelper.GetListItemType(value);
                    if (type == null || !type.IsSubclassOf(typeof(Inaction.Criteria.CriteriaBase)))
                    {
                        MessageBox.Show("数据类型不是所识别的查询数据源类型！");
                    }
                }
                criteriaBindingSource = value;
            }
        }

        CriteriaBase criteria = null;
        [Browsable(false)]
        public CriteriaBase Criteria
        {
            get
            {
                if (criteria != null) return criteria;
                if (CriteriaBindingSource != null)
                    return CriteriaBindingSource.DataSource as CriteriaBase;
                return null;
            }
            set { criteria = value; }
        }

        [Description("数据源类型")]
        [Category("Inaction")]
        public Type ListType
        {
            get { return BindingHelper.GetListType(bindingSource); }
        }

        [Description("数据源元素类型")]
        [Category("Inaction")]
        public Type ItemType
        {
            get { return BindingHelper.GetListItemType(bindingSource); }
        }

        #endregion

        #region 其他
        internal Inaction.Core.ISavable SavableObj
        {
            get
            {
                if (bindingSource == null) return null;
                if (TypeIsList)
                {
                    return bindingSource.List as Inaction.Core.ISavable;
                }
                else
                {
                    return bindingSource.Current as Inaction.Core.ISavable;
                }
            }
        }
        internal Inaction.Business.IEntityList EntityList
        {
            get
            {
                if (bindingSource == null) return null;
                return bindingSource.List as Inaction.Business.IEntityList;
            }
        }
        internal Inaction.Business.IEntity CurrentBusiness
        {
            get
            {
                if (bindingSource == null) return null;
                return (Inaction.Business.IEntity)bindingSource.Current;
            }
        }
        internal bool IsDirty
        {
            get
            {
                return SavableObj != null && SavableObj.IsDirty;
            }
        }
        [Browsable(false)]
        internal bool HasData
        {
            get
            {
                if (EntityList != null) return EntityList.Count > 0;
                return SavableObj != null;
            }
        }
        #endregion

        #endregion

        #region 构造
        public BindingInfo()
        {
        }

        public BindingInfo(IContainer container)
        {
            container.Add(this);
        }
        #endregion

        #region 方法
        private void InitRootSource()
        {
            if (!DesignMode) return;
            var components = ((System.ComponentModel.Container)(this.GetService(typeof(System.ComponentModel.Design.IDesignerHost)))).Components;
            var enumerator = components.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current is BarManager)
                {
                    var barManager = enumerator.Current as BarManager;
                    barManager.InitRootSource();
                    break;
                }
            }
        }

        public override string ToString()
        {
            if (this.ListType == null) return base.ToString();
            return ListType.Name;
        }
        #endregion
    }
    [Serializable]
    public enum TransDataType
    {
        Customer,
        FirstTransData,
        LastTransData
    }
}